﻿app.controller('DownloadController', function ($scope, $http, $window, $compile, $filter, ipCookie, common, option) {
    $scope.result = {};
    $scope.dateOption = option.date;
    $scope.datetimeOption = option.datetime;

    $scope.GetAppList = function (searchmodel, page) {
        $scope.PageLoading = true;
        $scope.AppList = [];
        $http({
            method: 'POST',
            url: '../Download/FindDownloadAPPFileList',
            params: { 'page': page }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.TotalItemCount = ret.Data.TotalItemCount;
                    $scope.AppList = ret.Data.Data;
                    $scope.PageLoading = false;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {

            });
    }

    $scope.AddAppForm = function (model) {
        if (model.Version == null || model.Version=='') {
            alertify.alert("请输入版本号！");
            return false;
        }
        $scope.FormUploading = true;
        var formdata = new FormData(document.getElementById("UploadForm"));
        $http({
            method: 'POST',
            url: '../Download/AddDownloadAPPFile',
            data: formdata,
            transformRequest: angular.identity,
            headers: {
                'Content-Type': undefined
            }
        })
            .success(function (ret) {
                if (ret.Code == 100) {
                    $scope.FormUploading = false;
                    alertify.alert(ret.Msg);
                    setTimeout(function () {
                        window.location = 'AppList';
                    }, 2000)
                }
                else {
                    $scope.FormUploading = false;
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                common.error($scope);
            })
    }
});

app.filter('HideCardNo', function () { //可以注入依赖
    return function (text) {
        if (text != null && text != "")
            return text.replace(/^(.{3})(?:\d+)(.{4})$/, "$1******$2");
    }
});

app.filter("JSONDate", function ($filter) {
    return function (input, format) {
        if (input) {
            var r = input.replace(/[^0-9|-]+/g, '');
            var timestamp = Number(r);
            return $filter("date")(timestamp, format);
        }
        else {
            return input;
        }
    }
});